package com.ofo.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ofo.dao.BikePasswordDao;
import com.ofo.model.BikePassword;

@Repository("bikePasswordDao")
public class BikePasswordDaoImpl implements BikePasswordDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean addBikePassword(BikePassword bikePassword) {
		String sql = "INSERT INTO bike_password (`marksId`, `password`, `createUser`) VALUES(?,?,?)";
		return jdbcTemplate.update(sql, bikePassword.getMarks(), bikePassword.getPassword(),
				bikePassword.getCreateUser()) > 0;
	}

	@Override
	public BikePassword getBikePassword(Integer id) {
		String sql = "SELECT * FROM bike_password where id=?";
		try {
			return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<BikePassword>(BikePassword.class), id);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<BikePassword> getBikePasswordByMarks(Integer marksId) {
		try {
			String sql = "SELECT * FROM bike_password where marksId=?";
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper<BikePassword>(BikePassword.class), marksId);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<BikePassword> getBikePasswords() {
		try {
			String sql = "SELECT * FROM bike_password";
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper<BikePassword>(BikePassword.class));
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public void deleteBikePassword(Integer id) {
		String sql = "DELETE FROM bike_password where id=?";
		jdbcTemplate.update(sql, id);
	}

}
